<?php

namespace MLM\Controller;

use Zend\View\Model\ViewModel;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;

class WalletController extends \MLM\Model\Generic\Controller {

    

    public function ewalletAction() {

        $identity = $this->hasSession();
        $this->layout('layout/member');

        $getParams = $this->getRequest()->getQuery();
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        $this->layout()->navMenu = "<li class='active'>Wallet</li> <li class='active'>E-Wallet</li>";

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);
        $dataSaldo = $accStorage->getTotalWalletByPeriod($identity->member_id, $identity->member_code, "E", $date1, $date2);

        $view->setVariable('dataSaldo', $dataSaldo);

        return $view;
    }

    public function roiwalletAction() {

        $identity = $this->hasSession();
        $this->layout('layout/member');

        $getParams = $this->getRequest()->getQuery();
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        $this->layout()->navMenu = "<li class='active'>Wallet</li> <li class='active'>ROI Wallet</li>";

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);
        $dataSaldo = $accStorage->getTotalWalletByPeriod($identity->member_id, $identity->member_code, "ROI", $date1, $date2);

        $view->setVariable('dataSaldo', $dataSaldo);

        return $view;
    }


    public function referralAction() {

        $identity = $this->hasSession();
        $this->layout('layout/member');

        $getParams = $this->getRequest()->getQuery();
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        $this->layout()->navMenu = "<li class='active'>Wallet</li> <li class='active'>Referral Wallet</li>";

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);
        $dataSaldo = $accStorage->getTotalWalletByPeriod($identity->member_id, $identity->member_code, "REF", $date1, $date2);

        $view->setVariable('dataSaldo', $dataSaldo);

        return $view;
    }


    public function binaryAction() {

        $identity = $this->hasSession();
        $this->layout('layout/member');

        $getParams = $this->getRequest()->getQuery();
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        $this->layout()->navMenu = "<li class='active'>Wallet</li> <li class='active'>Binary Wallet</li>";

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);
        $dataSaldo = $accStorage->getTotalWalletByPeriod($identity->member_id, $identity->member_code, "BIN", $date1, $date2);

        $view->setVariable('dataSaldo', $dataSaldo);

        return $view;
    }


    public function shoppingAction() {

        $identity = $this->hasSession();
        $this->layout('layout/member');

        $getParams = $this->getRequest()->getQuery();
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');


        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        $this->layout()->navMenu = "<li class='active'>Wallet</li> <li class='active'>Shopping Wallet</li>";

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);
        $dataSaldo = $accStorage->getTotalWalletByPeriod($identity->member_id, $identity->member_code, "SHOP", $date1, $date2);

        $view->setVariable('dataSaldo', $dataSaldo);

        return $view;
    }


    public function totalsaldowalletAction(){
        $identity = $this->hasSession();
        $getParams = $this->getRequest()->getQuery();

        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $accNum = (isset($getParams['accNum'])) ? $getParams['accNum'] : "";
        $mutation = (isset($getParams['mutation'])) ? $getParams['mutation'] : "";
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);

        $dataSaldo = $accStorage->getTotalWalletByPeriod($identity->member_id, $identity->member_code, $transType, $date1, $date2);

        

                $tmp = '<table cellpadding="5px">
                    <tr>
                        <td>Total</td>
                        <td>: </td>
                        <td align="right"> '.number_format($dataSaldo['kredit']-$dataSaldo['debet'], 2, '.', ',') .'</td>
                    </tr>
                    
            </table>';


        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', $tmp);
        return $view;
    }
    

    //Get Messages Notif
    private function getMsgNotif($memberTypeId, $memberId){


        if ($memberId == 100) $memberId2 = $memberId;
        else{
            $identity = $this->hasSession();
            //$memberId2 = ($identity->member_reff_id == "") ? $memberId : $identity->member_reff_id;
            $memberId2 = $memberId;
            $isAgent = $identity->member_reff_id;
        }


        $notifStorage = new \MLM\Model\Notification\Storage($this->_db);
        $notifMsg = new \MLM\Model\Notification($notifStorage);
        $newMsgCnt = $notifMsg->getCountNewMsg($memberId);
        $newMsgList = $notifMsg->getNewMsgList($memberId);
        $newAlertCnt = $notifMsg->getCountAlertApproval($memberTypeId, $memberId2, $isAgent);
        $newAlertList = $notifMsg->getAlertApproval($memberTypeId, $memberId2, $isAgent);

        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        $control = $this->getEvent()->getRouteMatch()->getParam('controller');

        $this->layout()->menu = $control.'\\'.$action;
        $this->layout()->newMsgCnt = $newMsgCnt;
        $this->layout()->newMsgList = $newMsgList;
        $this->layout()->newAlertCnt = $newAlertCnt;
        $this->layout()->newAlertList = $newAlertList;

    }

    private function hasSession() {
        $auth = new AuthenticationService();

        $identity = $auth->getIdentity();

        if (!$auth->hasIdentity()) {
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }

}
